﻿using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using App.Common;
using QxFramework.Core;
using System;
public class TimeManager : LogicModuleBase, ITimeManager
{
    public TimeData timeData;
    public override void Init()
    {
        base.Init();
        if (!RegisterData(out timeData))
        {
            timeData.CurrentTime = 0;
        }
    }
    public void TimeGoing()
    {
        timeData.CurrentTime += Time.deltaTime;
    }
    public float GetCurrent()
    {
        return timeData.CurrentTime;
    }
    public void RgstTimeAction(float TimeLength, Action act)
    {
        CoroutineManager.Instance.StartCoroutine(RgstTimeActionIE(TimeLength, act));
    }
    public void RgstTimeAction(MonoBase m, float TimeLength, Action act)
    {
        CoroutineManager.Instance.StartCoroutine(RgstTimeActionIE(m, TimeLength, act));
    }
    private IEnumerator RgstTimeActionIE(float TimeLength, Action act)
    {
        while (TimeLength > 0)
        {
            if (IsTimeGoing())
            {
                TimeLength -= Time.deltaTime;
            }
            yield return 0;
        }
        act.Invoke();
        yield return 0;
    }
    private IEnumerator RgstTimeActionIE(MonoBase m, float TimeLength, Action act)
    {
        while (TimeLength > 0)
        {
            if (IsTimeGoing() && m.ItsYourTurn())
            {
                TimeLength -= Time.deltaTime;
            }
            yield return 0;
        }
        act.Invoke();
        yield return 0;
    }
    public bool IsTimeGoing()
    {
        return timeData.IsTimeGoing;
    }
    public override void LateUpdate()
    {
        base.LateUpdate();
        if (!GameMgr.Get<IBattleManager>().GetBattleData().InBattle)
        {
            timeData.IsTimeGoing = true;
            return;
        }
        if (timeData.CurrentTimeTemp != GetCurrent())
        {
            timeData.CurrentTimeTemp = GetCurrent();
            timeData.IsTimeGoing = true;
        }
        else
        {
            timeData.IsTimeGoing = false;
        }
    }
}
public class TimeData: GameDataBase
{
    public float CurrentTime;
    public float CurrentTimeTemp;
    public bool IsTimeGoing;
}
